<?php
class Admin_Model_News extends Zend_Db_Table_Abstract{
	protected $_name = 'content';
	
	public function getAllNews()
	{
		$select = $this->select()->setIntegrityCheck(false)
						->from(array('n' => 'content'),'*')
						->joinLeft(array('c'=> 'category'),'n.categoryId = c.categoryId');
		$result = $this->fetchAll($select);
		return $result;
	}
	
	public function fetchPaginatorAdapter()
	{
		$select = $this->select()->setIntegrityCheck(false)
						->from(array('n' => 'content'),'*')
						->joinLeft(array('c'=> 'category'),'n.categoryId = c.categoryId');
		$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
		return $adapter;
	}
	
	public function updateNews($id,$subject,$categories_id,$description,$content)
	{
		$row = $this->find($id)->current();
		if ($row)
		{
			$row->news_subject = $subject;
			$row->categories_id = $categories_id;
			$row->news_shortcontent = $description;
			$row->news_content = $content;
			
			$row->save();
		}
	}
	
	public function addNews($headline,$category_id,$description,$content)
	{
		$row = $this->createRow();
		$row->title = $headline;
		$row->summary = $description;
		$row->body = $content;
		$row->categoryId = $category_id;
		
		$row->save();
		$id = $this->_db->lastInsertId();
		return $id;
	}
	
	public function deleteNews($newsid)
	{
		$row = $this->find($newsid)->current();
		if ($row)
		{
			$row->delete();
			return true;
		}
		else
		{
			throw new Zend_Exception("Could not find news with id = ".$newsid);
		}
	}
}